const { asyncFetch } = require('../public/javascripts/tools')
const city = 'chengdu'
const position = '1'
const URL = `https://www.lagou.com/quanguo-zhaopin`
const queryList = [
    'Java',
    'webqianduan',
    'C',
    'rengongzhineng',
    'Python',
    'qianrushi',
    'ceshi',
    'Android',
    'yunwei'
]


// 解决 superagent 中文乱码问题
const charset = require('superagent-charset');
const superagent = charset(require('superagent'));
require('superagent-proxy')(superagent);

const headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko',
    'Accept-Language': 'zh-Hans-CN, zh-Hans;q=0.5',
    'cookie': 'smidV2=202102011617243bea2ae5ca124d20089dcdbecacd199900e54b04b663d31f0; LG_LOGIN_USER_ID=a3b8339331f64ef3b2f39cc88629be3a18c9fb23b57644a15666671876614755; LG_HAS_LOGIN=1; EDUJSESSIONID=ABAAAECABCAAACDF5326EE418AAFD9D3BC43E2D0A0495F9; user_trace_token=20210219103251-36cafa2b-9ed4-468a-b5b8-17fe7c091aef; thirdDeviceIdInfo=%5B%7B%22channel%22%3A1%2C%22thirdDeviceId%22%3A%22WHJMrwNw1k/FSAVPc4cE7IK8n3Mar6S/Ff8cYUHj8ku1+R0ZtCp9KvTA87aSdzsfVZFqGZbIy9TCH5AtQGeUl9CR75Qu7xsY2dCW1tldyDzmQI99+chXEikb0kzoFqb2o9lCUKKcsmkTaFO8webhNijYmmmXo8LlTkQE5YcNLqNriNYPfoOP/bkLXs3lyVHOLrkEhLMclyymD6qx8eP9SqjiM5iSMAfwcEksSMMjOlKR0dSAT76Cng+8bH85TScs11487582755342%22%7D%2C%7B%22channel%22%3A2%7D%5D; sensorsdata2015session=%7B%7D; user-finger=0a4d5a013cde42889e3d7bfbb4da1ee2; LGUID=20210219103321-41e95f5b-3fe4-499d-a0d5-820a3c0ebae8; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1613702002; _ga=GA1.2.953427008.1613702002; _gid=GA1.2.2048702824.1613702002; X_MIDDLE_TOKEN=c24bb6e21c19bbca3dd57bc68f50171f; index_location_city=%E5%85%A8%E5%9B%BD; __lg_stoken__=de637c61966ef3954c33fe278aa81ed4ae8bd5bf1e2cd2cf5b373cb1c7abc987a08222cf08fcb2ba9398712b376a962aef095023776a06daf074ae340a1a2580867a7ea20878; _gat=1; PRE_UTM=; PRE_HOST=; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2Fquanguo-zhaopin%2FC%2F2%2F%3FfilterOption%3D2%26sid%3D1474f757ee584ca5b26253a144a37e65; LGSID=20210219135400-6d344b38-b013-4f73-be13-d054478f5c70; PRE_SITE=; X_HTTP_TOKEN=fa2e7917edf6c2f19044173161767492f98c76148f; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2214280369%22%2C%22first_id%22%3A%221775caafb619ee-08c06ef53a50ba-326f7006-1296000-1775caafb62cfc%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24os%22%3A%22MacOS%22%2C%22%24browser%22%3A%22Chrome%22%2C%22%24browser_version%22%3A%2287.0.4280.141%22%7D%2C%22%24device_id%22%3A%221775caafb619ee-08c06ef53a50ba-326f7006-1296000-1775caafb62cfc%22%7D; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1613714410; LGRID=20210219140009-7ffbd2e6-6b51-4a68-95cc-3042e0fbf469',
    origin: 'https://www.lagou.com',
    referer: 'https://www.lagou.com/'
}

const data = {
    'first': 'false',
    'pn': '3',
    'kd': 'java'
}
/**
 * index.js
 * [description] - 使用superagent.get()方法来访问
 */
function fetch () {
  let result = []
  return new Promise((resolve,reject) => {
    superagent.get(URL).charset('utf8').set(headers).end(async (err, res) => {
      if (err) {
        console.log(`热点新闻抓取失败 - ${err}`)
      } else {
       result = await getHotNews(res)
       resolve(result)
      }
    });
    //asyncFetch(URL, queryList, 1, 1, headers)
  })
}

Array.prototype.choice = function() {
    const len = this.length
    if(len === 0) return this[len]
    return this[Math.floor(Math.random()*len)]
}

const cheerio = require('cheerio');

let getHotNews = (res) => {
  let hotNews = [];

  var $ = cheerio.load(res.text, {decodeEntities: false});
  // 找到目标数据所在的页面元素，获取数据
  $('.item_con_list').find('li').each((idx, ele) => {
   if(ele.type  === 'tag' && ele.name === 'li') {
    hotNews.push(ele.attribs)         
   }  
  });
  return hotNews
};


module.exports = fetch